// pages/cun/cun.js
import {
  areaList
} from '@vant/area-data';
import {
  school,
  random,
  pay
} from '../request/wx';
let videoAd = null

Page({

  /**
   * 页面的初始数据
   */
  data: {
    areaList,
    school: '',
    showAge: false,
    showSchool: false,
    showSchoolList: false,
    ageList: ['0-18', '18-22', '22-26', '26-30', '30-40','40-50','50-60'],
    schoolList: [],
    age: '',
    columns: ['白羊座', '金牛座', '双子座', '巨蟹座', '狮子座', '处女座', '天秤座', '天蝎座', '射手座', '摩羯座', '水瓶座', '双鱼座'],
    showArea: false,
    showXz: false,
    city: '',
    info: {},
    xingzuo: '',
    sex: 0,
	shenhe:2
  },
  radioChange(e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    this.setData({
      sex: e.detail.value
    })
  },
  onChange(e) {
    this.setData({
      xingzuo: e.detail.value,
      showXz: false
    })
  },
  closeXz() {
    this.setData({
      showXz: false
    })
  },
  showCity() {
    this.setData({
      showArea: true
    })
  },
  showAge() {
    this.setData({
      showAge: true
    })
  },
  showSchool() {
    this.setData({
      showSchool: true
    })
  },
  closeAge() {
    this.setData({
      showAge: false
    })
  },
  closeSchool() {
    this.setData({
      showSchool: false
    })
  },
  showXz() {
    this.setData({
      showXz: true
    })
  },
  onChangeAge(e) {
    console.log(e);
    this.setData({
      age: e.detail.value,
      showAge: false
    })
  },
  onChangeSchool(e) {
    console.log(e);
    this.setData({
      school: e.detail.value.name,
      showSchool: false
    })
  },
  areaConfig(e) {
    console.log(e);
    let value = e.detail.values
    let add = ''
    value.map(v => {
      add += v.name + ' '
    })
    this.setData({
      city: add,
      showArea: false
    })
    school({
      city: add
    }).then(res => {
      if (res.data.length > 0) {
        this.setData({
          showSchoolList: true,
          schoolList: res.data
        })
      }
    })
  },
  closeArea() {
    console.log(1);
    this.setData({
      showArea: false
    })
  },
  submit() {
    let {
      age,
      school,
      wxaccount,
      xingzuo,
      city,
      sex
    } = this.data
    let that = this
    if (!age) {
      wx.showToast({
        title: '请选择年龄',
        icon: 'none'
      })
      return
    }
    if (!sex) {
      wx.showToast({
        title: '请选择性别',
        icon: 'none'
      })
      return
    }
    let ageWhere = ''
    if (age.indexOf('以上') != -1) {
      ageWhere = {
        min: 30
      }
    } else {
      let data = age.split('-')
      ageWhere = {
        min: data[0],
        max: data[1]
      }
    }
    let config = wx.getStorageSync('config')
    console.log(config);
    random({
      ...ageWhere,
      school,
      xingzuo,
      city,
      sex
    }).then(res => {
      if (res.code == 200) {
        that.setData({
          info: res.data,
          haveInfo: false
        })

        if (config.isPay == 1) {
          let openid = wx.getStorageSync('openid');
          let type = 1;
          pay({
            openid,
            type
          }).then(pay => {
            console.log(pay);
            wx.requestPayment({
              timeStamp: pay.timeStamp,
              nonceStr: pay.nonceStr,
              package: pay.packageValue,
              signType: pay.signType,
              paySign: pay.paySign,
              success: function (result) {
                that.setData({
                  info: res.data,
                  haveInfo: true
                })
              },
              fail(err) {
                wx.showToast({
                  title: '支付失败',
                  icon: 'none'
                })
              }
            });
          })
        } else if (config.isPay == 2) {
          if (videoAd) {
            videoAd.onClose(close => {
              console.log(close, '关闭');
              // 用户点击了【关闭广告】按钮
              if (close && close.isEnded) {
                this.setData({
                  info: res.data,
                  age: '',
                  school: '',
                  wxaccount: '',
                  xingzuo: '',
                  city: '',
                  haveInfo: true
                })
                // 正常播放结束，可以下发游戏奖励
              } else {
                wx.showToast({
                  title: '退出广告,不展示数据',
                  icon: 'none'
                })
                // 播放中途退出，不下发游戏奖励
              }
            })
            videoAd.show().catch(() => {
              // 失败重试
              videoAd.load()
                .then(() => videoAd.show())
                .catch(err => {
                  console.log('激励视频 广告显示失败')
                })
            })
          }
        }else{
          this.setData({
            info: res.data,
            age: '',
            school: '',
            wxaccount: '',
            xingzuo: '',
            city: '',
            haveInfo: true
          })
        }
      } else {
        wx.showToast({
          title: '无匹配信息',
          icon: 'none'
        })
      }
    })
  },
  adload(e) {
    console.log(e, '1');
  },
  aderr(e) {
    console.log(e, '2');
  },
  copy(e) {
    wx.setClipboardData({
      data: this.data.info.wxaccount,
      success(res) {
        console.log(res);
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let config = wx.getStorageSync('config')
    if (config.isPay == 0) {
      if (wx.createRewardedVideoAd) {
        videoAd = wx.createRewardedVideoAd({
          adUnitId: config.quid
        })
        videoAd.onLoad(() => {
          console.log('激励视频 广告加载成功')
        })
        videoAd.onError((err) => {
          console.log(err)
        })
      }
    }
	
	this.setData({
		shenhe: config.shenhe
	})

  },
  onClose() {
    this.setData({
      haveInfo: false
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})